import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    dialogVisible: false,
    centerDialogVisible: false,
    totalPage: 0,
    pagedata: [],
    input: '',
    updateData: {},
    produce_workshop: [{
      id: '',
      name: '',
      localtion: '',
      remark: '',
      gmt_create: '',
      gmt_modify: ''
    }],
    cunchu: [{
      id: '',
      name: '',
      localtion: '',
      remark: '',
      gmt_create: '',
      gmt_modify: ''
    }],
    newdata: {}
  },
  mutations: {// 更改共享状态的方法
    workshoplist(state, payload) {
      state.produce_workshop = payload.data
      state.cunchu = payload.data
      console.log(state.produce_workshop, '2')
    },
    xiugai(state, payload) {
      state.updateData = payload.data
      console.log(state.updateData, 'sd')
    },
    fanye(state, payload) {
      console.log(state.cunchu.length)
      state.totalPage = state.cunchu.length / 5
      if (!isInteger(state.cunchu.length / 5)) {
        state.totalPage = state.cunchu.length / 5 + 1
      }
      console.log((payload.data - 1) * 5, payload.data * 5)
      state.pagedata = state.cunchu.slice((payload.data - 1) * 5, payload.data * 5)
      // for (let i = (payload.data - 1) * 5; i < payload.data * 5; i++) {
      //   // const element = array[i]
      //   console.log(state.produce_workshop[i].id)
      // }
      state.produce_workshop = state.pagedata
      console.log(state.pagedata, state.cunchu)
      function isInteger(obj) {
        return Math.round(obj) === obj // 是整数，则返回true，否则返回false
      }
    }
  },
  actions: {// 在需要封装多个Mutation方法时，使用
    doActionChange(context) {
      context.commit('doChange')
      context.commit('doChange2')
    }
  }
})
